library WhitePhospherous initializer init requires LegacySystem


	private function Trig_White_Phospherous_Conditions takes nothing returns boolean
		if ( not ( GetSpellAbilityId() == 'A02G' ) ) then
			return false
		endif
		return true
	endfunction

	// creates trueshot aura effect
	private function createTargetWP takes unit caster, real x, real y returns unit
		local player p = GetOwningPlayer(caster)
		local unit u = CreateUnit(p, 'e005', x,y,0.0)
		set p = null
		return u
	endfunction

	private function createWPCloud takes unit caster, real x, real y, real duration returns unit
		local player p = GetOwningPlayer(caster)
		local unit u = CreateUnit(p, 'e00L', x,y,0.0)
		call DestroyEffect( AddSpecialEffect("Effect_Artillery.mdx", x, y ) )
		call UnitApplyTimedLifeBJ( duration, 'BTLF', u )
		set p = null
		return u
	endfunction

	private function playArtillerySoundWP takes unit origin returns nothing
		 call AttachSoundToUnit(gg_snd_Artillery,origin)
		 call SetSoundVolume(gg_snd_Artillery,127)
		 call StartSound(gg_snd_Artillery)
	endfunction

	private function playArtillerySoundWPDrop takes unit origin returns nothing
		 call AttachSoundToUnit(gg_snd_WillyPeter,origin)
		 call SetSoundVolume(gg_snd_WillyPeter,127)
		 call StartSound(gg_snd_WillyPeter)
	endfunction


	private function Trig_White_Phospherous_Actions takes nothing returns nothing
	   // variables
		local unit caster = GetTriggerUnit()
		local real x = GetSpellTargetX()
		local real y = GetSpellTargetY()
		local integer total_shells = getInteger(null,"FO_total_shells")
		local integer i = 0
		local real wp_duration = 40.0
		local integer number_of_shells = 5
		local real barrage_delay = I2R(getInteger(null,"FO_barrage_delay"))
		local player p = GetOwningPlayer(GetTriggerUnit())
		
		local unit target_aura
	   
		// core
		// check for shells and substract number per barrage
		if(number_of_shells > total_shells) then 
			 call print ("Shells depleted")  
			 set target_aura = null
			 set caster = null
			 return
		else
			 call putInteger(null,"FO_total_shells", total_shells - number_of_shells)
			 call print("Shells Remaining: " + I2S(total_shells - number_of_shells))
		endif 

		if GetRandomInt(0,4) == 1 then
			call FieldChat( caster, 3.0, udg_Pcolors[GetConvertedPlayerId(p)] + GetPlayerName(p) + "|r : Whiskey Pete!" )
		endif
	
			
		//DangerZone Dummy
		set target_aura  = CreateUnit(Player(14),'n00P',x, y,0.0) 
		call UnitApplyTimedLife(target_aura ,'Bhwd',barrage_delay+3.0+wp_duration)
	 
		// create target aura
		set target_aura = createTargetWP(caster, x,y)  
		call UnitApplyTimedLife(target_aura ,'Bhwd', barrage_delay)
		call PingMinimapEx(x,y,3,255,255,255,false)

		call PolledWait(barrage_delay-2.0)
	   
		// play sound 2 seconds before impact
		call playArtillerySoundWP(target_aura)

		call PolledWait(2.0)

		// drop the cloud 

		set target_aura = createWPCloud(caster,x, y,wp_duration)  
		call playArtillerySoundWPDrop(target_aura)

		// cleanup
		set target_aura = null
		set caster = null
		set p = null
	endfunction

	//===========================================================================
	private function init takes nothing returns nothing
		local trigger t
		set t = CreateTrigger(  )
		call TriggerRegisterAnyUnitEventBJ( t, EVENT_PLAYER_UNIT_SPELL_EFFECT )
		call TriggerAddCondition( t, Condition( function Trig_White_Phospherous_Conditions ) )
		call TriggerAddAction( t, function Trig_White_Phospherous_Actions )
	endfunction


endlibrary